#!/usr/bin/env python

#Extract peptide-protein interactions from DOMINO database
#only fit for the peptide with no id, only peptide

from BeautifulSoup import BeautifulSoup

import urllib, csv, re

ns = '{http://uniprot.org/uniprot}'
#res = urllib2.urlopen('http://www.uniprot.org/uniprot/Q9NZW5.xml')
domino_table = csv.reader(open('2009-10-22-domino-pdz-binary.mitab26'),
                          delimiter='\t')
#csvreader.
uni_proteinID = set()


for row in domino_table:
    #iiden = row[13] #Interaction identifier(s)
    uni_proteinID.add(row[0].split(':')[1]) #interactor ID A
    
    #get a set of protein ID aka uniprot acc
    #then check the interaction page for each protein
    
    


for iter in uni_proteinID:
    #iter = 'Q14160'
    params = urllib.urlencode({'proteinAlias': iter})
    f = urllib.urlopen('http://mint.bio.uniroma2.it/domino/search/search.do',
                       params)
    
    #print f.read()
    soup = BeautifulSoup(''.join(f.read()))
    #aa is a temp of the file
    aa = soup.find(attrs={"name":"interactorAc"})
    
    
    params = urllib.urlencode({'interactorAc': aa["value"]})
    f = urllib.urlopen(
        'http://mint.bio.uniroma2.it/domino/search/interactionsCompact.do',
        params)
    
    soup = BeautifulSoup(''.join(f.read()))
    
    #print "-------------"
    #print soup.find(attrs={"name":"interactionsForm"})
    #print soup.find(attrs={"name":"interactionsForm"}).findPreviousSibling()
    
    aa = soup.find('pre')
    while (aa != None ):
        i = aa.findNext('input')["value"]
        #id_seq[i]=aa.string
        print iter, i, aa.string 
        aa = aa.findNext('pre')
        
    #Find if there's a "view peptide" button
    peptide_form = soup('form', {'action' : re.compile('/domino/search/peptides.do.*?')})
    
    if peptide_form != []:
        for inputs in peptide_form:
            mintids = inputs.fetch('input', {'value' : re.compile('MINT-.*?')})
            for mint in mintids:
                #print mint['value']
                params = urllib.urlencode({'interactorAc': mint['value']})
                f = urllib.urlopen(
                    'http://mint.bio.uniroma2.it/domino/search/peptides.do',
                    params)
                #interactions = ''.join(f.read())
                interactions = csv.reader(f, delimiter='\t')
                
                #peptides are different, but some of them are identical at the last
                #five position, should consider how to use them.
                for row in interactions:
                    try:
                        dominoid = row[0]
                        peptide = row[6]
                        uniprotid = row[3].split(':')[1]
                        print uniprotid, dominoid, peptide
                    except(IndexError):
                        #should be the first line gives error
                        pass
    #break
    
  
  
    
#print soup.find(attrs={"value":"DOMINO-6274115"})4.findParents(limit=1)

#print soup.prettify()